
#include <BVBoxLayout>
#include <BGridLayout>
#include <BLabel>

#include "FormatDialog.h"

FormatDialog::FormatDialog(BWidget* parent) : BDialog(parent)
{
	_codeFormatGroupBox = new BGroupBox();
	{
		_countSpinBox = new BSpinBox();
		_fontLineEdit = new BLineEdit();
		_colorButton = new BButton(Graph_Round_Fill);
		_groundButton = new BButton(Graph_Round_Fill);
		BGridLayout* gridLayout = new BGridLayout(_codeFormatGroupBox);
		gridLayout->addWidget(0, 0, new BLabel(u8"数量"));
		gridLayout->addWidget(0, 1, _countSpinBox);
		gridLayout->addWidget(1, 0, new BLabel(u8"字体"));
		gridLayout->addWidget(1, 1, _fontLineEdit);
		gridLayout->addWidget(2, 0, new BLabel(u8"颜色"));
		gridLayout->addWidget(2, 1, _colorButton);
		gridLayout->addWidget(3, 0, new BLabel(u8"背景"));
		gridLayout->addWidget(3, 1, _groundButton);
	}

	_lineFormatGroupBox = new BGroupBox();
	{
		_alignButton = new BButton();
		_heightSpinBox = new BSpinBox();
		_spacingSpinBox = new BSpinBox();
		BGridLayout* gridLayout = new BGridLayout(_lineFormatGroupBox);
		gridLayout->addWidget(0, 0, new BLabel(u8"对齐方式"));
		gridLayout->addWidget(0, 1, _alignButton);
		gridLayout->addWidget(1, 0, new BLabel(u8"行高"));
		gridLayout->addWidget(1, 1, _heightSpinBox);
		gridLayout->addWidget(2, 0, new BLabel(u8"行间距"));
		gridLayout->addWidget(2, 1, _spacingSpinBox);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addWidget(_codeFormatGroupBox);
	vlayout->addWidget(_lineFormatGroupBox);

}
FormatDialog::~FormatDialog()
{

}

bool FormatDialog::setAsset(const BValue& asset)
{
	if (this->asset() != asset)
	{
		if (TextFormat* format = this->asset())
			disconnect(format);
		BObject::setAsset(asset);
		if (TextFormat* format = this->asset())
		{
			connect(format, Signal_Clear, &FormatDialog::slotFormatClear);
			connect(format, Signal_Changed, &FormatDialog::slotFormatChanged);
			connect(format, "code-item-changed", &FormatDialog::slotCodeItemChanged);
			connect(format, "line-item-changed", &FormatDialog::slotLineItemChanged);
		}
		return true;
	}
	return false;
}

void FormatDialog::freshCodeItem()
{
	if (const CodeItem* codeItem = _codeFormatGroupBox->asset())
	{
		_countSpinBox->setValue(codeItem->count);
		if (codeItem->font)
			_fontLineEdit->setText(codeItem->font->family());
		_colorButton->setColor(codeItem->color);
		_groundButton->setColor(codeItem->ground);
	}
}
void FormatDialog::freshLineItem()
{
	if (const LineItem* lineItem = _lineFormatGroupBox->asset())
	{
		_alignButton->setAsset(lineItem->align);
		_heightSpinBox->setValue(lineItem->height);
		_spacingSpinBox->setValue(lineItem->spacing);
	}
}

void FormatDialog::slotFormatClear(BObject* object, const BValue& value)
{
	_codeFormatGroupBox->setAsset(value);
	_lineFormatGroupBox->setAsset(value);
}
void FormatDialog::slotFormatChanged(BObject* object, const BValue& value)
{
	freshCodeItem();
	freshLineItem();
}
void FormatDialog::slotCodeItemChanged(BObject* object, const BValue& value)
{
	_codeFormatGroupBox->setAsset(value);
	freshCodeItem();
}
void FormatDialog::slotLineItemChanged(BObject* object, const BValue& value)
{
	_lineFormatGroupBox->setAsset(value);
	freshLineItem();
}
